package net.caixiaomi.info.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;

import net.caixiaomi.info.R;


public class NotificationHelper extends ContextWrapper {
    public static final String percent = "%";
    private static final int DOWNLOAD_APK_ID = 1001;
    private NotificationManager mNotifyManager;
    private Notification.Builder mBuilder;

    public NotificationHelper(Context base) {
        super(base);
        getManager();
        getBuilder();
    }

    public void notifyDownLoad(int progress, boolean isFinish) {
        String contentText = "";
        if(isFinish){
            progress = 100;
            contentText = "完成";
        }else{
            contentText = progress + percent;
        }
        getBuilder().setProgress(100, progress, false);
        getBuilder().setContentText(contentText);
        getManager().notify(DOWNLOAD_APK_ID, mBuilder.build());

    }
    public void setPendingIntent(Intent intent){
        PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(),0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
        getBuilder().setContentIntent(pendingIntent);
    }

    private Notification.Builder getBuilder() {
        if (mBuilder == null) {
            mBuilder = new Notification.Builder(this);
            mBuilder.setContentTitle(getString(R.string.app_name))
                    .setSmallIcon(R.mipmap.ic_launcher);
        }
        return mBuilder;
    }

    private NotificationManager getManager() {
        if (mNotifyManager == null) {
            mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mNotifyManager;
    }
}
